import { Head } from "fresh/runtime"; import { HttpError } from "fresh"; import { page, type PageProps } from "fresh"; import { define } from "utils/state.ts"; import { checkToken } from "utils/server.ts"; import { find } from "utils/db.ts"; import TopBar from "../islands/TopBar.tsx"; import Editor, { EditorMode } from "../islands/Editor.tsx"; import PageContainer from "../components/layout/PageContainer.tsx"; interface PostProps { id: string; title: string; content: string; shared: boolean; isLogined: boolean; allowMode: EditorMode; } export const handler = define.handlers({ GET(ctx) { const tokenUserId = checkToken(ctx.req); const postId = ctx.params.id; const post = find( "Post", tokenUserId ? { id: postId, user_id: tokenUserId } : { id: postId, shared: 1 }, ["title", "content", "shared"], ); if (post.length > 0) { return page({ id: postId, isLogined: Boolean(tokenUserId), allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read, title: post[0]["title"] as string, content: post[0]["content"] as string, shared: post[0]["shared"] === 1, }); } throw new HttpError(404); }, }); export default function Post(props: PageProps) { return ( <> {props.data.title} ); }